// pages/setting/mangerEntrance/newplan/index.js
import config_data from '../../../../utils/config'

import {
  $wuxToptips
} from '../../../../dist/index'
import {
  $wuxCalendar
} from '../../../../dist/index'

var _post_json = require("../../../../utils/network.js")._post_json;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    planDate: [],
    planNoticeTime: "",
    remark: "",
    submit_btn_loading: false
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  clickSelectDate() {
    const hide = $wuxToptips().show({
      hidden: true,
      text: '请选择周报日期',
      duration: 3000
    });
    $wuxCalendar().open({
      value: this.data.planDate,
      onChange: (values, displayValues) => {
        console.log('onChange', values, displayValues)

        if (values.length != 0) {
          hide();
        }
        this.setData({
          planDate: displayValues,
        })
      },
    })
  },
  bindNoticeTimeChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      planNoticeTime: e.detail.value
    })
  },
  bindInputRemark: function(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  submit() {
    console.log('备注值', this.data.remark)
    //防止重复提交
    if (this.data.submit_btn_loading) {
      return;
    }

    this.setData({
      submit_btn_loading: true
    })

    let that = this;
    wx.getStorage({
      key: 'openId',
      success: function(res) {
        _post_json("weeklyPlan/newPlan", {
          "openId": res.data,
          "remark": that.data.remark,
          "planNoticeTime": that.data.planNoticeTime,
          "planDate": that.data.planDate[0]
        }).then((res) => {
          let respCode = res.data.respCode;
          let respMsg = res.data.respMsg;
          if (respCode == 10001) {
            $wuxToptips().success({
              hidden: true,
              text: "计划成功",
              duration: 3000,
              success() {
                wx.navigateBack({
                  delta: 1
                })
              },
            })
          }
        }, (res) => {
          that.setData({
            submit_btn_loading: false
          })
          let respCode = res.data.respCode;
          let respMsg = res.data.respMsg;
          if (respCode == 4001 || respCode == 404 || respCode == 405) {
            $wuxToptips().error({
              hidden: true,
              text: respMsg,
              duration: 3000,
              success() {},
            })
          }
          if (respCode == undefined || respCode == 500) {
            $wuxToptips().error({
              hidden: true,
              text: "服务器严重错误",
              duration: 3000,
              success() {},
            })
          }
        });
      }
    })
  }
})